package com.ruoyi.web.controller.chargepile;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.chargepile.domain.ChargeConfig;
import com.ruoyi.chargepile.domain.ChargePile;
import com.ruoyi.chargepile.domain.ChargeRecords;
import com.ruoyi.chargepile.domain.dto.RepairRecordDto;
import com.ruoyi.chargepile.domain.enums.ChargePileStatusEnum;
import com.ruoyi.chargepile.domain.vo.ChargePileCountByStatusVo;
import com.ruoyi.chargepile.domain.vo.ChargeRecordsMonitorVo;
import com.ruoyi.chargepile.domain.vo.MonitorVo;
import com.ruoyi.chargepile.domain.vo.RepairRecordVo;
import com.ruoyi.chargepile.service.ChargePileService;
import com.ruoyi.chargepile.service.ChargeRecordsService;
import com.ruoyi.chargepile.service.RepairRecordService;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.util.DateRange;
import com.ruoyi.util.Pages;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.NumberFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import static java.lang.Integer.parseInt;

/**
 * @program: ruoyi
 * @description: 数据监控控制层
 * @author: XINGchi0130
 * @create: 2024-07-11 09:20
 **/
@RestController
@RequestMapping("/monitor")
public class MonitorController {

    @Autowired
    private ChargePileService chargePileService;

    @Autowired
    private ChargeRecordsService chargeRecordsService;

    @Autowired
    private RepairRecordService repairRecordService;

    @GetMapping("/getMonitor")
    public AjaxResult getMonitor() throws ParseException {
        MonitorVo monitorVo = new MonitorVo();

        // 充电桩list
        List<ChargePile> chargePileList = chargePileService.list();
        monitorVo.setChargePileList(chargePileList);

        // 充电桩各个状态数量
        monitorVo.setChargePileInUseCount(0);
        monitorVo.setOnlineRate(0);
        List<ChargePileCountByStatusVo> countList = chargePileService.getChargePileStatusByTypeVo();
        for (ChargePileCountByStatusVo item : countList){
            // 空闲
            if(item.getStatus().equals(ChargePileStatusEnum.IDLE.getCode())){
                monitorVo.setChargePileIdleCount(item.getCounts());
            }
            // 使用中
            if(item.getStatus().equals(ChargePileStatusEnum.IN_USE.getCode())){
                monitorVo.setChargePileInUseCount(item.getCounts());
            }
            // 维修中
            if(item.getStatus().equals(ChargePileStatusEnum.IN_MAINTENANCE.getCode())){
                monitorVo.setChargePileInMaintenanceCount(item.getCounts());
            }
            // 停用
            if(item.getStatus().equals(ChargePileStatusEnum.OUT_OF_SERVICE.getCode())){
                monitorVo.setChargePileOutOfServiceCount(item.getCounts());
            }
        }

        // 使用率
        NumberFormat numberFormat = NumberFormat.getNumberInstance();
        numberFormat.setMaximumFractionDigits(0);
        String result = numberFormat.format((float) monitorVo.getChargePileInUseCount() / (float) chargePileList.size() * 100);
        monitorVo.setOnlineRate(parseInt(result));

        // 近七天的充电记录次数
        Date date;
        String formatDate;
        List<String> dateList = new ArrayList<>();
        List<Integer> chargeRecordsCount = new ArrayList<>();
        Date paseBeginTime;
        Date paseEndTime;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        for (int i = 0; i < 7; i++) {
            // 得到时间
            date = DateUtils.addDays(new Date(), -i);
            formatDate = sdf.format(date);
            dateList.add(formatDate);
            //定义每天的24h时间范围
            String beginTime = formatDate + " 00:00:00";
            String endTime = formatDate + " 23:59:59";
            paseBeginTime = dateFormat.parse(beginTime);
            paseEndTime = dateFormat.parse(endTime);
            // 得到充电记录数量
            LambdaQueryWrapper<ChargeRecords> chargeRecordsLambdaQueryWrapper = new LambdaQueryWrapper<>();
            chargeRecordsLambdaQueryWrapper
                    .between(ChargeRecords::getChargeStartTime, paseBeginTime, paseEndTime);
            int count = Math.toIntExact(chargeRecordsService.count(chargeRecordsLambdaQueryWrapper));
            chargeRecordsCount.add(count);
        }
        Collections.reverse(dateList);
        Collections.reverse(chargeRecordsCount);
        ChargeRecordsMonitorVo chargeRecordsMonitorVo = new ChargeRecordsMonitorVo();
        chargeRecordsMonitorVo.setDateList(dateList);
        chargeRecordsMonitorVo.setChargeRecordsCount(chargeRecordsCount);
        monitorVo.setChargeRecordsMonitorVo(chargeRecordsMonitorVo);

        // 最近7天维修情况
        DateRange dateRange = new DateRange();
        DateRange dateRangeCompleted = new DateRange();
        RepairRecordDto repairRecordDto = new RepairRecordDto();
        LocalDateTime endTime = LocalDateTime.now();
        dateRange.setEndTime(LocalDate.from(endTime));
        LocalDateTime beginTime = endTime.minusDays(7);
        dateRange.setBeginTime(LocalDate.from(beginTime));
        Pages pages = new Pages();
        pages.setPageNum(0);
        pages.setPageSize(10000000);
        List<RepairRecordVo> repairRecordVoList = repairRecordService.listRepairRecord(pages, dateRange, dateRangeCompleted, repairRecordDto);
        monitorVo.setRepairRecordVoList(repairRecordVoList);

        return AjaxResult.success(monitorVo);
    }

}
